package com.shh.aiojbackendserver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shh.aiojbackendserver.app.StudyAssistApp;
import com.shh.aiojbackendserver.common.BaseResponse;
import com.shh.aiojbackendserver.common.ErrorCode;
import com.shh.aiojbackendserver.common.ResultUtils;
import com.shh.aiojbackendserver.constant.UserConstant;
import com.shh.aiojbackendserver.exception.BusinessException;
import com.shh.aiojbackendserver.exception.ThrowUtils;
import com.shh.aiojbackendserver.manager.GenerationLimitManager;
import com.shh.aiojbackendserver.model.dto.QuestionAi.QuestionAiAddRequest;
import com.shh.aiojbackendserver.model.dto.QuestionAi.QuestionAiQueryRequest;
import com.shh.aiojbackendserver.model.entity.QuestionAi;
import com.shh.aiojbackendserver.model.entity.User;
import com.shh.aiojbackendserver.model.vo.QuestionAiVO;
import com.shh.aiojbackendserver.service.QuestionAiService;
import com.shh.aiojbackendserver.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;


/**
 * ai生成题目接口
 */

@RestController
@RequestMapping("/ai")
public class StudyAssistAIController {

    @Resource
    private GenerationLimitManager generationLimitManager;

    @Resource
    private QuestionAiService questionAiService;

    @Resource
    private StudyAssistApp studyAssistApp;

    @Resource
    private UserService userService;


    /**
     * 使用ai创建题目并加载到数据库中
     * @param questionAiAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long>  addQuestion(@RequestBody QuestionAiAddRequest questionAiAddRequest, HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        String userRole = loginUser.getUserRole();
        if(generationLimitManager.canGenerate(userId,userRole )){
            QuestionAi questionAi = studyAssistApp.doChat(questionAiAddRequest);
            questionAiService.validQuestion(questionAi, true);
            questionAi.setUserId(userId);
            questionAi.setFavourNum(0);
            questionAi.setThumbNum(0);
            boolean result = questionAiService.save(questionAi);
            ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
            long newQuestionId = questionAi.getId();
            return ResultUtils.success(newQuestionId);
        }
        return ResultUtils.error(ErrorCode.OPERATION_ERROR,"今日生成次数已用完,开通vip获取更多机会");
    }

    @GetMapping("/del")
    public BaseResponse<Boolean> delQuestionAi(Long id,HttpServletRequest httpServletRequest){
        if(id < 0 ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long createUserId = questionAiService.getById(id).getUserId();
        User user = userService.getLoginUser(httpServletRequest);
        //如果删除的人不是本人和管理员
        if(!createUserId.equals(user.getId()) && !userService.isAdmin(httpServletRequest)){
            throw new BusinessException(ErrorCode.FORBIDDEN_ERROR);
        }
        Boolean result = questionAiService.removeById(id);
        return ResultUtils.success(result);
    }

    /**
     * 获取ai生成题目的封装类
     * @param questionAiQueryRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/get/quesitonAiVO")
    public BaseResponse<Page<QuestionAiVO>> getQuestionAIList (@RequestBody QuestionAiQueryRequest questionAiQueryRequest,HttpServletRequest httpServletRequest){
            User user = userService.getLoginUser(httpServletRequest);
            long userId = user.getId();
            long current = questionAiQueryRequest.getCurrent();
            long pageSize =   questionAiQueryRequest.getPageSize();
            questionAiQueryRequest.setUserId(userId);
            Page<QuestionAi> questionAiPage = questionAiService.page(new Page<>(current,pageSize),
                    questionAiService.getQueryWrapper(questionAiQueryRequest));

            return ResultUtils.success(questionAiService.getQuestionAiVOPage(questionAiPage));
    }


    /**
     * 获取题目封装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionAiVO> getQuestionAiById(Long id,HttpServletRequest request){
        if(id < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QuestionAi questionAi = questionAiService.getById(id);
        if (questionAi == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(questionAiService.getQuestionAiVO(questionAi,request));
    }


}
